#!/bin/sh

git_base_url="git@code-sh.huawei.com:EulerOS"
repo_base_path="/usr1/merge_code_to_huawei/EulerOS_V2R3C00_${GERRIT_BRANCH}/gerrit_code"
cur_dir=`pwd`
output="${WORKSPACE}/check_result_${GERRIT_BRANCH}.txt"
error_ret="${WORKSPACE}/error_${GERRIT_BRANCH}.txt"

cd ${repo_base_path}/.repo/manifests
git pull
cd -

if [ "x$GERRIT_BRANCH" == "xnext" ];then
    cp ${repo_base_path}/.repo/manifests/source_next.xml .
    cp ${repo_base_path}/.repo/manifests/open_source_next.xml .
    cat source_next.xml > next.xml
    cat open_source_next.xml >> next.xml
    sed -i 's/EulerOS_V2R3C00_//g' next.xml
    input_list=${cur_dir}/next.xml
else
    if [ "x$GERRIT_BRANCH" == "xmaster" ];then
        cp ${repo_base_path}/.repo/manifests/default.xml .
        sed -i 's/EulerOS_V2R3C00_//g' default.xml
        input_list=${cur_dir}/default.xml
    else
        cp ${repo_base_path}/.repo/manifests/${GERRIT_BRANCH}.xml .
        sed -i 's/EulerOS_V2R3C00_//g' ${GERRIT_BRANCH}.xml
        input_list=${cur_dir}/${GERRIT_BRANCH}.xml
    fi
fi

compare_dir="${cur_dir}/compare_dir"

if [ -d compare_dir ];then
    rm -rf $compare_dir/*
else
    mkdir $compare_dir
fi

cd $compare_dir

echo "Check Start: " > $output
echo "Check Start: " > $error_ret
while read line
do
    if ! echo $line | grep "<project name" &> /dev/null;then
        continue
    fi
    echo "--------------------------------------------------------" >> $output
    module_name=`echo $line | awk -F '"' '{print $2}'`
    module_path=`echo $line | awk -F '"' '{print $4}'`
    echo $module_name >> $output
    cd $repo_base_path
    repo sync -c $module_path --no-tags
    if [ -d ${repo_base_path}/${module_path} ];then
        cd ${repo_base_path}/${module_path}
        if git branch | grep $GERRIT_BRANCH &> /dev/null;then
            git checkout $GERRIT_BRANCH
        else
            git checkout -b $GERRIT_BRANCH origin/${GERRIT_BRANCH}
        fi
        git pull --no-tags
    else
        echo "Exception 1" >> $output
        echo "Path ${repo_base_path}/${module_path} not exist" >> $output
        echo "Path ${repo_base_path}/${module_path} not exist" >> $error_ret
        continue
    fi
    cd $compare_dir
    if git clone ${git_base_url}/${module_name}.git &> /dev/null;then
        echo "git clone ok" >> $output
    else
        echo "git clone failed" >> $output
        echo "$module_name : git clone failed" >> $error_ret
        continue
    fi
    cd $module_name
    if git branch | grep $GERRIT_BRANCH &> /dev/null;then
        git checkout $GERRIT_BRANCH
    else
        git checkout -b $GERRIT_BRANCH origin/${GERRIT_BRANCH}
    fi
    git pull

    #ls . | grep -v ".git" | xargs rm -rf
    cd $compare_dir
    cp -a ${repo_base_path}/${module_path} gerrit_code_dir
    rm -rf gerrit_code_dir/.git
    if [ -d huawei_code_dir ];then
        rm -rf huawei_code_dir
    fi
    cp -a $module_name huawei_code_dir
    rm -rf huawei_code_dir/.git
    #cp -a gerrit_code_dir/* .
    #rm -rf gerrit_code_dir
    ##cp -a ${module_name}/.git gerrit_code_dir/
    ##rm -rf $module_name
    ##mv gerrit_code_dir $module_name
    ##cd $module_name

    save_flag=0
    ###if git status | grep "nothing to commit" &> /dev/null;then
    if diff -rq gerrit_code_dir huawei_code_dir &> /dev/null;then
        echo "OK" >> $output
    else
    #    if git status | grep "deleted:" &> /dev/null;then
    #        git status | grep "deleted:" | awk '{print $3}' | xargs git rm -rf
    #    fi
    #    git add .
    #    git commit -m "merge form gerrit"
    #    git push
    #    echo "Merged" >> $output
    #    echo "$module_name : Merge" >> $error_ret
    #fi
    #if ! git status | grep "nothing to commit" &> /dev/null;then
        echo "NO" >> $output
        echo "$module_name : NO" >> $error_ret
        save_flag=1
    fi

    cd $compare_dir
    if [ -d gerrit_code_dir ];then
        rm -rf gerrit_code_dir
    fi
    if [ -d huawei_code_dir ];then
        rm -rf huawei_code_dir
    fi
    if [ -d $module_name ];then
        if [ $save_flag -eq 0 ];then
            rm -rf $module_name
        fi
    else
        echo "Exception 2" >> $output
        echo "$module_name : Exception 2" >> $error_ret
    fi
done < $input_list
